//
//  TTTabBar.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/19.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTTabBar: UITabBar {

    var centerButotnBlock:(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }
    
    func setUpUI(){
        
        addSubview(centerButton)
        
            }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let widt = self.frame.width * 0.2
        var index : CGFloat = 0
        for value in self.subviews{
            
            
            if value .isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.origin.x = widt * index
                
                value.frame.size.width = widt
                
                index++
                
                if index == 2 {
                    
                    index++
                }
            }
        }
        
        centerButton.center.x = self.frame.width * 0.5
        centerButton.center.y = self.frame.height * 0.5

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var centerButton:UIButton = {
       
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "didCenterButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        return button
    }()
    
    func didCenterButton(){
        
        centerButotnBlock?()
    }
}
